package capricornus.net.loader
{
	import capricornus.components.utils.MovieClipUtils;
	import capricornus.events.ResLoaderEvent;
	
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;

	/**
	 * 加载图片或者swf，swf不要加载as2的，不然报错 
	 */	
	[Event(name="init", type="capricornus.events.ResLoaderEvent")]
	[Event(name="progress", type="capricornus.events.ResLoaderEvent")]
	[Event(name="complete", type="capricornus.events.ResLoaderEvent")]
	public class ResLoader extends EventDispatcher
	{
		protected var _loader:Loader;
		protected var _url:String;
		private var _removeStageMovieClip:Boolean;
		
		/**
		 * 
		 * @param removeStageMovieClip 是否停止并清除swf场景里面的的所有元件，默认是false
		 * 
		 */		
		public function ResLoader( removeStageMovieClip:Boolean=false )
		{
			_removeStageMovieClip = removeStageMovieClip;
			init();
		}
		
		private function init():void
		{
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onLoadComplete, false, 0, true );
			_loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, onProgress, false, 0, true );
			_loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, onError, false, 0, true );
			_loader.contentLoaderInfo.addEventListener( Event.INIT, onLoadInit, false, 0, true );
		}
		
		public function destroy( unloadAndStop:Boolean=false ):void
		{
			if( _loader != null )
			{
				removeListener();
				try{ _loader.close() }catch( e:Error ){};
				
				if( unloadAndStop )
				{
					try{ _loader.unloadAndStop() } catch( e:Error ){};
				}
				else
				{
					try{ _loader.unload() } catch( ee:Error ){};
				}
			}
		}
		
		public function set url( value:String ):void
		{
			_url = value;
		}
		
		public function get url():String
		{
			return _url;
		}
		
		public function get data():*
		{
			var c:* = null;
			try
			{
				c = _loader.content;
			}
			catch( e:Error ){};
			
			return c;
		}
		
		public function load( url:String, context:LoaderContext = null ):void
		{
			_url = url;
			_loader.load( new URLRequest( url ), context );
		}
		
		public function loadBytes( byte:ByteArray, context:LoaderContext=null ):void
		{
			_loader.loadBytes( byte, context );
		}
		
		private function removeListener():void
		{
			_loader.contentLoaderInfo.removeEventListener( Event.COMPLETE, onLoadComplete );
			_loader.contentLoaderInfo.removeEventListener( ProgressEvent.PROGRESS, onProgress );
			_loader.contentLoaderInfo.removeEventListener( IOErrorEvent.IO_ERROR, onError );
			_loader.contentLoaderInfo.removeEventListener( Event.INIT, onLoadInit );
		}
		
		private function onLoadInit( e:Event ):void
		{
			dispatchEvent( new ResLoaderEvent( ResLoaderEvent.INIT ) );
		}
		
		private function onProgress( e:ProgressEvent ):void
		{
			dispatchEvent( new ResLoaderEvent( ResLoaderEvent.PROGRESS, e.bytesLoaded/e.bytesTotal ) );
		}
		
		private function onError( e:Event ):void
		{
			dispatchEvent( new ResLoaderEvent( ResLoaderEvent.COMPLETE ) );
		}
		
		private function onLoadComplete( e:Event ):void
		{
			var li:LoaderInfo = _loader.contentLoaderInfo;
			var event:ResLoaderEvent = new ResLoaderEvent( ResLoaderEvent.COMPLETE );
			if( li.contentType=="application/x-shockwave-flash" )
			{
				if( li.actionScriptVersion < 3 )
				{
					throw new Error( "请不要加载as2版本的swf："+_url );
					/*event.data = _loader;
					removeListener();*/
				}
				else
				{
					event.data = li.content;
					if( event.data != null && _removeStageMovieClip )
					{
						var mc:Sprite = event.data as Sprite;
						MovieClipUtils.stopMC( mc );
						while( mc.numChildren > 0 )
						{
							mc.removeChildAt(0);
						}
					}
				}
			}
			else
			{
				event.data = li.content;
			}
			event.width = li.width;
			event.height = li.height;
			
			dispatchCompleteEvent( event );
		}
		
		protected function dispatchCompleteEvent( e:ResLoaderEvent ):void
		{
			dispatchEvent( e );
		}
	}
}








